const cheerio = require('cheerio')
const http = require('http')
var url = 'http://www.17500.cn/getData/ssq.TXT'
var fs = require('fs');
var path = require('path')
const del = require('del')//npm del

del.sync(path.resolve(__dirname,'../../dist/ssq.TXT'))
console.log(1)
function getData(http, url, next) {
	http.get(url, res => {
		res.setEncoding('utf8')
		var html = ''
		//获取页面数据
		res.on('data', data => {
			html += data
		})
		res.on('end', () => {
			// console.log(html)
			const filename = 'ssq.TXT';
			const savePath = path.resolve(__dirname, '../../dist', filename)
			fs.writeFileSync(savePath, html, 'utf8')
			console.log('下载完毕')
			// next()
		})

	}).on('error', () => {
		console.info('获取数据出错')
	})
}

// new Promise(resolve => {
// 	getData(http, url, resolve)
// }).then(() => {
// 	console.log(filename + '下载完毕')
// })
console.log(2)
getData(http,url)
// var request = require('request');
//
// /*
// * url 网络文件地址
// * filename 文件名
// * callback 回调函数
// */
//
// function downloadFile(uri,filename,callback){
// 	var savePath = path.resolve(__dirname,'../../dist',filename)
// 	var stream = fs.createWriteStream(savePath);
// 	request('utf-8',uri).pipe(stream).on('close', callback);
// }
//
// var fileUrl  = 'http://www.17500.cn/getData/ssq.TXT';
// var filename = 'ssq.TXT';
//
// new Promise(resolve => {
// 	downloadFile(fileUrl,filename,function(){
// 		resolve()
// 	});
//
// }).then(()=>{
// 	console.log(filename+'下载完毕');
// })

// downloadFile(fileUrl,filename,function(){
// 	console.log(filename+'下载完毕');
// });
